﻿// 3. Write a program that compares two char arrays lexicographically (letter by letter).

using System;

class Compare2CharArrays
{
    static void Main()
    {
        // get the length of first array
        Console.Write("Enter length of the first array : ");
        string size1 = Console.ReadLine();
        int sizeOfArray1;
        while (!int.TryParse(size1, out sizeOfArray1))
        {
            Console.Write("Invalid input. Enter again length for the first array : ");
            size1 = Console.ReadLine();
        }

        // get the length of second array
        Console.Write("Enter length of the second array : ");
        string size2 = Console.ReadLine();
        int sizeOfArray2;
        while (!int.TryParse(size2, out sizeOfArray2))
        {
            Console.Write("Invalid input. Enter again length for the second array : ");
            size2 = Console.ReadLine();
        }

        if (sizeOfArray1 <= 0 || sizeOfArray2 <= 0)
        {
            // if the values for length are negative or 0 they cannot be lengths for an array
            Console.WriteLine("The length of an array should be positive number. Start your program again.");
        }
        else
        {
            if (sizeOfArray1 < sizeOfArray2)
            {
                // if the length of the first array is bigger than the length of the second array
                Console.WriteLine("The first array is lexicographically earlier than the second one.");
            }
            else if (sizeOfArray1 > sizeOfArray2)
            {
                // if the length of the second array is bigger than the length of the first array
                Console.WriteLine("The second array is lexicographically earlier than the first one.");
            }
            else
            {
                bool earlierArray1 = false;
                bool earlierArray2 = false;
                char[] array1 = new char[sizeOfArray1];
                char[] array2 = new char[sizeOfArray2];

                for (int index = 0; index < sizeOfArray1; index++)
                {
                    // get the element of the first array
                    Console.Write("Enter element of the first array : ");
                    string element1 = Console.ReadLine();
                    char elementOfArray1;
                    while (!char.TryParse(element1, out elementOfArray1))
                    {
                        Console.Write("Invalid input. Enter element again : ");
                        element1 = Console.ReadLine();
                    }
                    array1[index] = elementOfArray1;

                    // get the element of the second array
                    Console.Write("Enter element of the second array : ");
                    string element2 = Console.ReadLine();
                    char elementOfArray2;
                    while (!char.TryParse(element2, out elementOfArray2))
                    {
                        Console.Write("Invalid input. Enter element again : ");
                        element2 = Console.ReadLine();
                    }
                    array2[index] = elementOfArray2;

                    if ((int)elementOfArray1 < (int)elementOfArray2)
                    {
                        // if the entered elements are different then the boolean variable gets value false
                        earlierArray1 = true;
                        break;
                    }
                    else if ((int)elementOfArray1 > (int)elementOfArray2)
                    {
                        earlierArray2 = true;
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }

                // print the result after comparing the arrays' elements lexicographically
                if (earlierArray1)
                {
                     Console.WriteLine("The first array is lexicographically earlier than the second one.");
                }
                else if (earlierArray2)
                {
                    Console.WriteLine("The second array is lexicographically earlier than the first one.");
                }
                else
                {
                    Console.WriteLine("Both arrays are equal.");
                }
            }
        }
    }
}
